package com.getinstaliker.instagram;

import android.support.v4.app.FragmentActivity;

import com.getinstaliker.activity.BaseFragmentActivity;
import com.getinstaliker.instagram.InstagramBaseTask.InstagramTaskListener;
import com.getinstaliker.instagram.model.AccessTokenModel;
import com.getinstaliker.screen.LoginFragment;

/**
 * The Class InstaImpl.
 * 
 * @author HuanND
 */
public class InstagramLogin {

    /** The m auth authentication listener. */
    private AuthAuthenticationListener mAuthAuthenticationListener;

    /** The m act context. */
    private FragmentActivity mActContext;

    /**
     * Instantiates a new insta impl.
     * 
     * @param context the context
     * @param authAuthenticationListener the auth authentication listener
     */
    public InstagramLogin(FragmentActivity context, AuthAuthenticationListener authAuthenticationListener) {
        this.mActContext = context;
        mAuthAuthenticationListener = authAuthenticationListener;
        showInstaLoginScreen();
    }


    /**
     * Show insta login dialog.
     */
    private void showInstaLoginScreen() {
        	AuthLoginListener instaAuthLoginListener = new AuthLoginListener() {

                @Override
                public void onError(String error) {
                    mAuthAuthenticationListener.onFail(error);
                }

                @Override
                public void onComplete(String token) {
                    setUpInstaTokenTask(token);
                }
            };
            if(mActContext instanceof BaseFragmentActivity){
            	((BaseFragmentActivity)mActContext).switchContent(new LoginFragment(instaAuthLoginListener), true);
            }
       }

    /**
     * Sets the up insta token task.
     * 
     * @param code the new up insta token task
     */
    public void setUpInstaTokenTask(String code) {
        InstagramUtil.executeAccessTokenTask(mActContext, code,
                new InstagramTaskListener<AccessTokenModel>() {

                    @Override
                    public void onTaskComplete(AccessTokenModel ob) {
                        if (ob != null) {
                            SessionStore.getInstance().saveInstagramSession(ob.access_token, ob.user.id,
                                    ob.user.username, ob.user.full_name);
                            mAuthAuthenticationListener.onSuccess();
                        }
                    }
          });
    }

    /**
     * Sets the auth authentication listener.
     * 
     * @param authAuthenticationListener the new auth authentication listener
     */
    public void setAuthAuthenticationListener(AuthAuthenticationListener authAuthenticationListener) {
        this.mAuthAuthenticationListener = authAuthenticationListener;
    }

    /**
     * The listener interface for receiving authAuthentication events. The class that is interested
     * in processing a authAuthentication event implements this interface, and the object created
     * with that class is registered with a component using the component's
     * <code>addAuthAuthenticationListener<code> method. When
     * the authAuthentication event occurs, that object's appropriate
     * method is invoked.
     * 
     * @see AuthAuthenticationEvent
     */
    public interface AuthAuthenticationListener {

        /**
         * On success.
         */
        public abstract void onSuccess();

        /**
         * On fail.
         * 
         * @param error the error
         */
        public abstract void onFail(String error);
    }

    /**
     * The listener interface for receiving authDialog events. The class that is interested in
     * processing a authDialog event implements this interface, and the object created with that
     * class is registered with a component using the component's
     * <code>addAuthDialogListener<code> method. When
     * the authDialog event occurs, that object's appropriate
     * method is invoked.
     * 
     * @see AuthDialogEvent
     */
    public interface AuthLoginListener {

        /**
         * On complete.
         * 
         * @param token the token
         */
        public void onComplete(String token);

        /**
         * On error.
         * 
         * @param error the error
         */
        public void onError(String error);
    }

}
